﻿using Dapper;
using FRMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FRMS.Helper
{
    public class SqlHelper<T>
    {
        public static bool SqlExecute(string strsql)
        {
            /* try
             {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                int count = conn.Execute(strsql);

                if (count >= 0)
                {
                    return true;
                }
                return false;
            }
            /*}
            catch
            {
                return false;
            }*/
        }

        public static bool SqlSessionExecute(string strsql)
        {
            /* try
             {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                int count = conn.Execute(strsql);

                if (count > 0)
                {
                    return true;
                }
                return false;
            }
            /*}
            catch
            {
                return false;
            }*/
        }

        public static int SqlExecuteRows(string strsql)
        {
            /*try
            {*/
            using (var conn = DataFactory.GetSQLConnection())
            {

                int count = conn.Execute(strsql);

                return count;
            }
            /*}
            catch
            {
                return -1;
            }*/
        }

        public static List<T> SqlList(string strsql)
        {
            using (var conn = DataFactory.GetSQLConnection())
            {
                /*try
                {*/
                CommFN<T> fn = new CommFN<T>();
                return fn.GetListData(strsql);
                /* }
                 catch
                 {
                     return null;
                 }*/
            }
        }

        public static int SqlScalar(string strsql)
        {
            int count = 0;
            /* try
             {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                count = conn.ExecuteScalar<int>(strsql);
            }
            /*}
            catch (Exception ex)
            {
                count = 0;
            }*/
            return count;
        }

        public static T SqlSingleData(string strsql)
        {
            /*try
            {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                CommFN<T> fn = new CommFN<T>();
                return fn.GetData(strsql);
            }
            /*}
            catch
            {
                return default(T);
            }*/
        }

        public static bool SqlDelete(string strsql)
        {
            /*try
            {*/
            using (var conn = DataFactory.GetSQLConnection())
            {

                int count = conn.Execute(strsql);

                if (count >= 0)
                {
                    return true;
                }
                return false;
            }
            /* }
             catch
             {
                 return false;
             }*/
        }

        public static string SqlScalarString(string strsql)
        {
            string str;
            /*try
            {*/
            using (var conn = DataFactory.GetSQLConnection())
            {
                str = conn.ExecuteScalar<string>(strsql);
            }
            /* }
             catch
             {
                 str = "";
             }*/
            return str;
        }
    }
}
